{% extends "layout/base.html" %}

{% block title %}Product audit{% endblock %}

{% block head %}
    {{ super() }}
    <style type="text/css">
        .bad {
            color: #cc9424;
        }

        .data-table tr:hover {
            background-color: #f0f0f0;
        }
    </style>
{% endblock %}
{% block panel %}

{% endblock %}
{% block content %}
    {% from "layout/macros.html" import query_param_list, show_raw_document %}
    <div class="panel highlight">
        <h1>Product Audit
            {%- if sentry_public_dsn and sentry_public_args.environment %} ({{ sentry_public_args.environment }})
            {%- endif -%}
        </h1>
        <div class="sub-header">{{ products_summarised|length }} products with summaries.
            {{ (products_missing | length) or 'None' }} without{% if products_missing %}:{% else %}.{% endif %}
            {% for name in products_missing %}
                {{ name | product_link }}{% if not loop.last %}, {% endif %}
            {% endfor %}
        </div>
    </div>
    <div class="panel">
        {% if spatial_quality_stats %}
            <table class="data-table unavailable-metadata" id="missing-metadata">
                <thead>
                <th>Product</th>
                <th class="missing">Missing footprint</th>
                <th class="missing">Missing CRS</th>
                <th class="feature"
                    title="Product has neither a grid specification, nor platform-specific regions (eg. Landsat WRS path rows)">
                    Missing Regions
                </th>
                <th class="feature">Missing Dataset Size</th>
                </thead>
                {% for stat in spatial_quality_stats | sort(attribute='product.name') %}
                    <tr class="search-result">
                        <td>{{ stat.product.name | product_link }}</td>
                        <td title="{{ stat.missing_footprint }} of {{ stat.count }} missing footprint"
                            class="missing-footprint">
                            {{ stat.missing_footprint | percent(stat.count) }}
                        </td>
                        <td title="{{ stat.missing_srid }} of {{ stat.count }} missing CRS"
                            class="missing-srid">
                            {{ stat.missing_srid | percent(stat.count) }}
                        </td>
                        <td title="{{ stat.count - stat.has_region }} of {{ stat.count }} without region specification"
                            class="missing-region">
                            {{ (stat.count - stat.has_region) | percent(stat.count) }}
                        </td>
                        <td title="{{ stat.count - stat.has_file_size }} of {{ stat.count }} without size_bytes field"
                            class="missing-file-size">
                            {{ (stat.count - stat.has_file_size) | percent(stat.count) }}
                        </td>
                    </tr>
                {% endfor %}
            </table>
        {% else %}
            <span class="primary-error">Spatial quality view not populated</span>
        {% endif %}
    </div>
    <div class="panel" id="largest-footprint-size">
        {% if spatial_quality_stats %}
            <h2>Largest footprint size</h2>
            Measured from binary storage. JSON/Metadata representation is much larger.
            <table class="data-table footprint-size">
                <thead>
                <th>Product</th>
                <th>Avg. Footprint size</th>
                </thead>
                {% for stat in spatial_quality_stats | sort(attribute='avg_footprint_bytes', reverse=True) | batch(15) | first %}
                    <tr class="search-result">
                        <td>{{ stat.product.name | product_link }}</td>

                        {% if stat.footprint_size %}
                            {% set avg_footprint_size=stat.footprint_size / stat.count %}
                            <td class="size-value {% if avg_footprint_size > 400 %}bad{% endif %}">
                                {{ avg_footprint_size | printable_data_size }}
                            </td>
                        {% else %}
                            <td></td>
                        {% endif %}
                    </tr>
                {% endfor %}
            </table>
        {% endif %}
    </div>

    {% if product_timings_iter %}
        <div class="panel" id="query-timings">
            <h2>Day query speed</h2>
            <p>Extremely slow queries usually indicate a misconfigured index for the product.</p>
            <ul>
                {% for timing in product_timings_iter %}
                    <li>
                        <strong>{{ timing.name | product_link }}</strong>
                        <span {% if timing.time_seconds and timing.time_seconds > 0.8 %}class="bad"{% endif %}>
                        {% if timing.time_seconds %}
                            {{ (timing.time_seconds * 1000) | int }}ms
                        {% else %}
                            (empty product)
                        {% endif %}
                    </span>
                    </li>
                {% endfor %}
            </ul>
        </div>
    {% else %}
        <div class="panel odd">
            <a href="{{ url_for('audit.product_audit_page', timings=True) }}#query-timings">
                View query timings (very slow)
            </a>
        </div>
    {% endif %}

{% endblock %}

